package net.sureon.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
@SuppressWarnings("unchecked")
public class ApplicationDAOImpl extends SqlMapClientDaoSupport implements
		ApplicationDAO {

	public Application getApplicationByID(long companyID, long applicationID) {
		Application app = new Application();
		app.setApplicationID(applicationID);
		app.setCompanyID(companyID);
		return (Application) getSqlMapClientTemplate().queryForObject(
				"getApplicationByID", app);
	}

	public long insertApplication(Application application) {
		Object o = getSqlMapClientTemplate().insert("insertApplication",
				application);
		if (o instanceof Long) {
			return ((Long) o);
		}
		return 0;
	}
	public List<Application> getApplicationByGroupNotReview(long companyID, List<Long> idsAsList, int skipResults, int maxResults) {
		if(idsAsList.size() > 0) {
			Application app = new Application();
			app.setIdsAsList(idsAsList);
			app.setCompanyID(companyID);
			return this.getSqlMapClientTemplate().queryForList("getApplicationByGroupNotReview", app, skipResults, maxResults);
		}
		return new ArrayList<Application>();
	}

	public List<Application> getApplicationByGroupReview(long companyID, List<Long> idsAsList, int skipResults, int maxResults) {
		if(idsAsList.size() > 0) {
			Application app = new Application();
			app.setIdsAsList(idsAsList);
			app.setCompanyID(companyID);
			return this.getSqlMapClientTemplate().queryForList("getApplicationByGroupReview", app, skipResults, maxResults);
		}
		return new ArrayList<Application>();
	}
	
	public List<Application> getApplicationByUserNotReview(long companyID,
			long userID,int skipResults,int maxResults) {
		Application app = new Application();
		app.setCompanyID(companyID);
		app.setUserID(userID);
		return this.getSqlMapClientTemplate().queryForList("getApplicationByUserNotReview", app, skipResults, maxResults);
	}

	/**
	 * 已审核通过的考勤
	 */
	public List<Application> getApplicationByUserReview(long companyID,
			long userID, int skipResults, int maxResults) {
		Application app = new Application();
		app.setCompanyID(companyID);
		app.setUserID(userID);
		return this.getSqlMapClientTemplate().queryForList("getApplicationByUserReview", app, skipResults, maxResults);
	}

	public int updateApplication(long companyID, long applicationID, java.util.Date reviewDate,
			long reviewedBy, String reviewNote, String status) {
		Application app = new Application();
		app.setCompanyID(companyID);
		app.setApplicationID(applicationID);
		app.setReviewDate(reviewDate);
		app.setReviewedBy(reviewedBy);
		app.setReviewNote(reviewNote);
		app.setStatus(status);
		return this.getSqlMapClientTemplate().update("updateApplication", app);
	}

	public int deleteApplication(long companyID, long applicationID) {
		Application app = new Application();
		app.setApplicationID(applicationID);
		app.setCompanyID(companyID);
		return this.getSqlMapClientTemplate().delete("deleteApplication", app);
	}

	public int editApplication(long companyID, long applicationID,
			String category, Date startDate, Date endDate, float sumTime,
			String note, java.util.Date submitDate) {
		int updateRows = 0;
		
		Application app = new Application();
		app.setCompanyID(companyID);
		app.setApplicationID(applicationID);
		app.setCategory(category);
		app.setStartDate(startDate);
		app.setEndDate(endDate);
		app.setSumTime(sumTime);
		app.setNote(note);
		app.setSubmitDate(submitDate);
		
		updateRows = this.getSqlMapClientTemplate().update("editApplication", app);
		if(updateRows > 0) {
			return updateRows;
		}
		return 0;
	}
}
